package com.sibu.yx.promotion.admin.web.support.model;

import lombok.Data;
import org.apache.shiro.authz.Permission;

import java.io.Serializable;

/**
 * @author     ：yuwenbo
 * @date       ：Created in 2019/4/19 15:33
 * @description：通过位来控制权限
 */
@Deprecated
@Data
public class BitPermission implements Permission,Serializable {

    //增删改查的权限
    private int read;

    private int write;

    private int update;

    private int delete;

    private static  int a=0xF00;


    private static  int b=0b01;




    //8421
    //delete,update,write,read这种
    public BitPermission(int bit){
        this.read=bit & 0b0001;
        this.write=bit >> 1 & 0b0001;
        this.update=bit >> 2 & 0b0001;
        this.delete=bit >> 3 & 0b0001;
    }

    @Override
    public boolean implies(Permission p) {
        if(!(p instanceof BitPermission)) return false;
        BitPermission bitPermission=(BitPermission)p;
        if(bitPermission.getRead()>this.read) return false;
        if(bitPermission.getWrite()>this.write) return false;
        if(bitPermission.getUpdate()>this.update) return false;
        return bitPermission.getDelete() <= this.delete;
    }

    public static void main(String[] args) {
        BitPermission bitPermission= new BitPermission(9);
        System.out.println(bitPermission);
    }

    public String toString() {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("delete:").append(delete).append(";")
                    .append("update:").append(update).append(";")
                    .append("write:").append(write).append(";")
                    .append("read:").append(read).append(";");
        return stringBuffer.toString();
    }


}
